package com.example.demo.config;

import com.example.demo.component.MyLocaleResolver;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//使用 WebMvcConfigurerAdapter 可以扩展 SpringMVC 的功能
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter{

//    /**
//     * 添加视图映射
//     * @param registry
//     */
//    @Override
//    public void addViewControllers(ViewControllerRegistry registry) {
////        super.addViewControllers(registry);
////        浏览器发生 /autoLogin 请求来到 login
//        registry.addViewController("/autoLogin").setViewName("login");
//    }

//    所有 WebMvcConfigurerAdapter 组件会一起起作用
    @Bean
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/login").setViewName("login");
                registry.addViewController("/report").setViewName("dashboard");
            }
        };
        return adapter;
    }

    /**
     * 区域信息解析器
     * @return
     */
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

}
